package com.clz.hyperit.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.clz.hyperit.dao.AnswerDao;
import com.clz.hyperit.dao.PositionDao;
import com.clz.hyperit.dao.QuestionDao;
import com.clz.hyperit.dao.QuestionTagDao;
import com.clz.hyperit.dao.TagDao;
import com.clz.hyperit.dao.UserDetailDao;
import com.clz.hyperit.entity.Answer;
import com.clz.hyperit.entity.Question;
import com.clz.hyperit.entity.QuestionTag;
import com.clz.hyperit.entity.Tag;
import com.clz.hyperit.entity.UserDetail;
import com.clz.hyperit.service.QuestionService;
import com.clz.hyperit.vo.Answer_User;
import com.clz.hyperit.vo.PageInfo;
import com.clz.hyperit.vo.Question_User;

@Service("questionService")
public class QuestionServiceImpl implements QuestionService {
	private static Logger logger = LoggerFactory
			.getLogger(QuestionServiceImpl.class);
	@Autowired
	private QuestionDao questionDao;
	@Autowired
	private UserDetailDao userDetailDao;
	@Autowired
	private TagDao tagDao;
	@Autowired
	private QuestionTagDao questionTagDao;
	@Autowired
	private AnswerDao answerDao;

	public Integer saveQuestion(Question question, String tag) {
		// 保存问题
		question.setAccept(0);
		question.setAnswer(0);
		question.setReading(0);
		return questionDao.save(question);
	}

	public Question findOneQuestion(Integer question_id) {
		return questionDao.get(question_id);
	}

	public void updateQuestion(Question question) {
		questionDao.updateWithNotNullProperties(question);
	}

	public List<Question_User> findAll(PageInfo pageInfo) {
		List<Question_User> q_u_list = new ArrayList<Question_User>();
		List<Question> question_list = questionDao.findAll(pageInfo);
		for (Question question : question_list) {
			UserDetail ud = userDetailDao.get(question.getUserDetail_id());
			q_u_list.add(setQustion_User(ud,question));
		}
		return q_u_list;
	}


	public List<Question_User> findPersonQuestion(PageInfo pageInfo,
			Integer userdetail_id) {
		List<Question_User> q_u_list = new ArrayList<Question_User>();
		List<Question> question_list = questionDao.findByuserdetail_id(pageInfo,userdetail_id);
		for(Question question:question_list){
			UserDetail ud = userDetailDao.get(question.getUserDetail_id());
			q_u_list.add(setQustion_User(ud,question));
		}
		return q_u_list;
	}
	
	//重复用的方法
	private Question_User setQustion_User(UserDetail ud,Question question){
		List<Tag> tag_list = new ArrayList<Tag>();
		List<QuestionTag> qt_list = questionTagDao.findByQuestionId(question.getId());
		for(QuestionTag qt:qt_list){
			tag_list.add(tagDao.get(qt.getTag_id()));
		}
		Question_User qu = new Question_User(ud.getName(),ud.getHead(),
				question.getId(), question.getTitle(),
				question.getContent(), question.getUserDetail_id(),
				question.getCreatetime(), question.getAccept(),
				question.getAnswer(), question.getReading(),tag_list);
		return qu;
	}

	public void deleteQuestion(Question question) {
		List<QuestionTag> qt_list = new ArrayList<QuestionTag>();
		qt_list =  questionTagDao.findByQuestionId(question.getId());
		if(qt_list.size()>0){
			updateTag_list(qt_list);
			deleteQt_list(qt_list);
		}
		questionDao.delete(question);
	}
	private void deleteQt_list(List<QuestionTag> qt_list) {
		for(QuestionTag qt:qt_list){
			questionTagDao.delete(qt);
		}
	}

	private void updateTag_list(List<QuestionTag> qt_list){
		for(QuestionTag qt: qt_list){
			Tag tag = tagDao.get(qt.getQuestion_id());
			tag.setQuestion(tag.getQuestion()-1);
			tagDao.updateWithNotNullProperties(tag);
		}
	}

	public Integer saveAnswer(Answer answer) {

		Question question = questionDao.get(answer.getQuestion_id());
		question.setAnswer(question.getAnswer()+1);
		questionDao.updateWithNotNullProperties(question);
		return answerDao.save(answer);
	}

	public Answer getOneAnswer(Integer id) {
		return answerDao.get(id);
	}

	public void deleteAnswer(Answer answer,Question questiton) {
		answerDao.delete(answer);
		questiton.setAnswer(questiton.getAnswer()-1);
		questionDao.updateWithNotNullProperties(questiton);
	}

	public List<Answer> getAnswer(PageInfo pageInfo, Integer question_id) {
		return answerDao.findByQustion_id(pageInfo,question_id);
	}

	public Answer saveBestAnswer(Integer answer_id, Integer question_id) {
		Answer answer = answerDao.get(answer_id);
		Question question = questionDao.get(question_id);
		question.setAccept(answer_id);
		questionDao.updateWithNotNullProperties(question);
		UserDetail ud = userDetailDao.get(answer.getUserDetail_id());
		ud.setAccept(ud.getAccept()+1);
		userDetailDao.updateWithNotNullProperties(ud);
		return answer;
	}

	public List<Question_User> findQuestionByAccept(PageInfo pageInfo,
			Integer is_accept) {
		List<Question_User> q_u_list = new ArrayList<Question_User>();
		List<Question> question_list = new ArrayList<Question>();
		if(is_accept!=0){
			question_list = questionDao.findQuestionByAccept_accept(pageInfo);
		}
		else{
			question_list = questionDao.findQuestionByAccept_notaccept(pageInfo);
		}
		for (Question question : question_list) {
			UserDetail ud = userDetailDao.get(question.getUserDetail_id());
			q_u_list.add(setQustion_User(ud,question));
		}
		return q_u_list;
	}

	public List<Question_User> getQuestionByAnswer(PageInfo pageInfo) {
		List<Question_User> q_u_list = new ArrayList<Question_User>();
		List<Question> question_list = new ArrayList<Question>();
		question_list = questionDao.getQuestionByAnswer(pageInfo);
		for (Question question : question_list) {
			UserDetail ud = userDetailDao.get(question.getUserDetail_id());
			q_u_list.add(setQustion_User(ud,question));
		}
		return q_u_list;
	}

	public List<Question_User> getQuestionByRead(PageInfo pageInfo) {
		List<Question_User> q_u_list = new ArrayList<Question_User>();
		List<Question> question_list = new ArrayList<Question>();
		question_list = questionDao.getQuestionByRead(pageInfo);
		for (Question question : question_list) {
			UserDetail ud = userDetailDao.get(question.getUserDetail_id());
			q_u_list.add(setQustion_User(ud,question));
		}
		return q_u_list;
	}

	public List<Question_User> SearchQuestion(PageInfo pageInfo, String search) {
		List<Question_User> q_u_list = new ArrayList<Question_User>();
		List<Question> question_list = new ArrayList<Question>();
		question_list = questionDao.SearchQuestion(pageInfo,search);
		for (Question question : question_list) {
			UserDetail ud = userDetailDao.get(question.getUserDetail_id());
			q_u_list.add(setQustion_User(ud,question));
		}
		return q_u_list;
	}
}
